package com.apabi.web.controller;

import com.apabi.common.pojo.JqGridResult;
import com.apabi.common.utils.LeeJSONResult;
import com.apabi.pojo.SysToken;
import com.apabi.service.SysTokenService;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by zhangxh.apabi on 2017/9/22.
 */
@Controller
@RequestMapping("/sysToken")
public class SysTokenController extends BaseController {

	final static Logger log = LoggerFactory.getLogger(SysTokenController.class);

	@Autowired
	private SysTokenService sysTokenService;

	/**
	 * 显示添加sys_token页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/createSysTokenPage")
	public ModelAndView createSysTokenPage(HttpServletRequest request) {
		log.debug("显示添加sys_token页面");
		ModelAndView mv = new ModelAndView("sysToken/createSysTokenPage");
		return mv;
	}
	
	
	/**
	 * sys_token页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/sysTokenList")
	public String showsysTokenListPage(HttpServletRequest request) {
		return "sysToken/sysTokenList";
	}


	/**
	 * 查询sys_token列表内容
	 * 
	 * @param article
	 * @param page
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/getSysTokenList")
	@ResponseBody
	public JqGridResult getUserInfoList(SysTokenController article, Integer page, Integer pageSize) {
		if (page == null) {
			page = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		JqGridResult result = sysTokenService.querySysTokenList(null, page, pageSize);

		return result;
	}

	/**
	 * 修改或增加sys_token
	 * 
	 * @param sysToken
	 * @return
	 */
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public LeeJSONResult saveOrUpdate(SysToken sysToken) {

		String sysTokenId = sysToken.getId();

		if (StringUtils.isEmpty(sysTokenId)) {
            sysToken.setToken(Sid.next());
            sysToken.setCreateTime(new Date());
			sysTokenService.saveSysToken(sysToken);
		} else {
			sysTokenService.updateSysTokenById(sysToken);
		}
		return LeeJSONResult.ok();
	}

	/**
	 * 查询某一个sys_token
	 * 
	 * @param sysTokenId
	 * @param request
	 * @return
	 */
	@RequestMapping("/sysTokenDetail")
	public ModelAndView sysTokenDetail(String sysTokenId, HttpServletRequest request) {

		SysToken sysToken = sysTokenService.querySysTokenById(sysTokenId);

		ModelAndView mv = new ModelAndView("sysToken/sysTokenDetail");
		mv.addObject("sysToken", sysToken);
		return mv;
	}

	/**
	 * 删除sys_token
	 * 
	 * @param sysTokenId
	 * @return
	 */
	@RequestMapping("/deleteSysToken")
	@ResponseBody
	public LeeJSONResult deleteSysToken(String sysTokenId) {

		if (StringUtils.isEmpty(sysTokenId)) {
			return LeeJSONResult.errorMsg("sys_tokenid为空或者不存在...");
		}
		sysTokenService.deleteSysTokenById(sysTokenId);
		return LeeJSONResult.ok();
	}

	/**
	 * 跳转到修改sys_token页面
	 * 
	 * @param sysTokenId
	 * @param request
	 * @return
	 */
	@RequestMapping("/modifySysToken")
	public ModelAndView modifySysToken(String sysTokenId, HttpServletRequest request) {

		SysToken sysToken = sysTokenService.querySysTokenById(sysTokenId);

		ModelAndView mv = new ModelAndView("sysToken/modifySysToken");
		mv.addObject("sysToken", sysToken);

		return mv;
	}
}
